﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Common.Basic;
using ECommerce.BLL.Member;
using ECommerce.Common;
using Jzy.BLL.SiteAdmin;
using Jzy.Model.SiteAdmin;
using Jzy.Core.Utility;
using ECommerce.Model.Member;
using ECommerce.BLL.MemberContact;
using ECommerce.Model.MemberContact;

namespace ECommerce.Web.Site.controls
{
    public partial class head : System.Web.UI.UserControl
    {
        private MemberBaseBO Member
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }

       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lnkRegister.NavigateUrl = "javascript:showForm('会员注册','" + GlobalVar.WebAppPath + "site/register.aspx',378,223)";
                lnkLogin.NavigateUrl = GlobalVar.WebAppPath + "site/login.aspx";
                lnkMyOrder.NavigateUrl = GlobalVar.WebAppPath + "site/MemberOrder.aspx";
                lnkMemberCenter.NavigateUrl = GlobalVar.WebAppPath + "site/MemberEdit.aspx";
                lnkJifen.NavigateUrl = GlobalVar.WebAppPath + "site/MemberIntegral.aspx";
                lnkCart.HRef = GlobalVar.WebAppPath + "site/shopping1.aspx";
                lnkNotice.HRef = GlobalVar.WebAppPath + "site/MemberContact.aspx";

                menu1.HRef = GlobalVar.WebAppPath + "default.aspx";
                menu2.HRef = GlobalVar.WebAppPath + "site/ProductList.aspx";

                if (Request.CurrentExecutionFilePath.ToLower().Contains("default.aspx"))
                {
                    menu1.Attributes["class"] = "sy";
                    menu2.Attributes["class"] = null;
                }
                else
                {
                    menu1.Attributes["class"] = null;
                    menu2.Attributes["class"] = "sy";
                }


                string shoppingCart = "";
                if (Request.Cookies["ShoppingCart"] != null)
                {
                    shoppingCart = Request.Cookies["ShoppingCart"].Value;
                }


                string[] idCounts = shoppingCart.Split(',');


                int productCount = 0;
                foreach (string idcount in idCounts)
                {
                    if (idcount != "")
                    {
                        productCount += int.Parse(idcount.Split('_')[1]);                        
                    }
                }
                lblCount.Text = productCount.ToString();

                txtProductName.Text = Request.QueryString["ProductName"];


                CompanyBO company = new CompanyBO();
                CompanyInfo info = company.GetList().First();
                string[] keys = info.SiteKeywords.Replace("，", ",").Trim(',').Split(',');
                foreach (string str in keys)
                {
                    litHotSearch.Text += "<a href='" + GlobalVar.WebAppPath + "site/productlist.aspx?ProductName=" + Server.UrlEncode(str) + "'>" + str + "</a> ";

                }

                //Response.Write(SitePage.IsTimeout().ToString());

                if (!SitePage.IsTimeout())
                {
                    bool flag = false;
                    MemberBaseInfo memInfo = this.Member.GetInfo(SiteSession.MemberID);
                    if (memInfo.Level != (int)MemberLevel.VIP)
                    {
                        imgVip.Visible = false;
                    }
                    else
                    {
                        imgVip.Visible = true;
                    }
                    DateTime compare = DateTime.Now.AddDays(1);
                    if (memInfo.Birthday != null)
                    {
                        if (memInfo.Birthday.Value.Day == compare.Day && memInfo.Birthday.Value.Month == compare.Month)
                        {
                            flag = true;
                        }
                    }
                    if (flag == false)
                    {
                        ECMemberContactBO contact = new ECMemberContactBO();
                        List<ECMemberContactInfo> contactList = contact.GetList(SiteSession.MemberID);

                        foreach (ECMemberContactInfo contactInfo in contactList)
                        {
                            if (contactInfo.MemorialDay.Day == compare.Day && contactInfo.MemorialDay.Month == compare.Month)
                            {
                                flag = true;
                                break;
                            }
                        }

                    }

                    if (flag)
                    {
                        JSUtil.ExcuteJS("flash()");
                    }
                }
                
            }
            if (SitePage.IsTimeout())
            {
                litWebcome.Visible = false;
                lnkLogout.Visible = false;

                lnkLogin.Visible = true;
                lnkRegister.Visible = true;
            }
            else
            {
                litWebcome.Visible = true;
                litWebcome.Text = "欢迎" + SiteSession.MemberLoginName + "登录999蛋糕网";
                lnkLogout.Visible = true;

                lnkLogin.Visible = false;
                lnkRegister.Visible = false;
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Member.ClearSession();

            Response.Redirect(Request.CurrentExecutionFilePath);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(GlobalVar.WebAppPath + "site/ProductList.aspx?ProductName=" + Server.UrlEncode(txtProductName.Text),true);
        }
    }
}